/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

/**
 *
 * @author anas
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

public class Statistiques {

    public static Connection getConnection() throws Exception {
        String driver = "org.gjt.mm.mysql.Driver";
        String url = "jdbc:mysql://localhost/Gestion_Chambres_Hotel";
        String username = "root";
        String password = "c56a59f2@";

        Class.forName(driver); // load MySQL driver
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
    }

    public static int countRows(Connection conn, String tableName) throws SQLException {
        // select the number of rows in the table
        Statement stmt = null;
        String status = "oui";
        ResultSet rs = null;
        int rowCount = -1;
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT COUNT(*) FROM chambre where STATUS_CHAMBRE='" + status + "'");
            // get the number of rows from the result set
            rs.next();
            rowCount = rs.getInt(1);
        } finally {
            rs.close();
            stmt.close();
        }
        return rowCount;
    }

    public static int countRows2(Connection conn, String tableName) throws SQLException {
        // select the number of rows in the table
        Statement stmt = null;
        String status = "non";
        ResultSet rs = null;
        int rowCount2 = -1;
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT COUNT(*) FROM chambre where STATUS_CHAMBRE='" + status + "'");
            // get the number of rows from the result set
            rs.next();
            rowCount2 = rs.getInt(1);
        } finally {
            rs.close();
            stmt.close();
        }
        return rowCount2;
    }

    public void Get_Infos() {

        Connection conn = null;
        try {
            conn = getConnection();
            String tableName = "chambre";
            System.out.println("tableName=" + tableName);
            System.out.println("conn=" + conn);
            System.out.println("rowCount=" + countRows(conn, tableName));

            DefaultPieDataset pieData = new DefaultPieDataset();
            pieData.setValue("Les Chambres Réserveé", new Integer(countRows(conn, tableName)));
            pieData.setValue("Les Chambres non Réserveé", new Integer(countRows2(conn, tableName)));
            pieData.setValue("Les Chambres hors service", new Integer(0));

            JFreeChart chart = ChartFactory.createPieChart3D("Pie Chart", pieData, true, true, true);
            PiePlot p = (PiePlot) chart.getPlot();
            //p.setForegroundAlpha(TOP_ALIGNMENT);
            ChartFrame frame = new ChartFrame("Statistiques", chart);
            frame.setVisible(true);
            frame.setSize(900, 1200);
            

        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        } finally {
            // release database resources
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {

    }
}
